﻿using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Web.Model;
using System.Linq;

namespace Web.DataAccess
{
	public class AllProjectsQuery
	{
        private User CurrentUser { get; set; }

        public AllProjectsQuery(User currentUser)
        {
            CurrentUser = currentUser;
        }

		public IEnumerable<Project> Load(ISession session)
		{
            var projects = session
                    .CreateCriteria<Project>()
                    .AddOrder(Order.Desc("Beginning"))
                    .List<Project>().Where(project => project.IsUpcomingOrInProgress || project.IsCompleted);

            if (CurrentUser != null && CurrentUser.IsAdmin)
            {
                return projects;
            }
            else
            {
                return projects.Where(p => p.IsVisibleToUser(CurrentUser)).ToList();
            }
		}
	}
}